package com.example.graduation.util;

import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Service
public class PdfUtil {
    private static final String FILE_PATH = "F:\\JAVA_CODING\\PDF\\";

    //定义全局的字体静态变量
    private static Font titlefont;
    private static Font headfont;
    private static Font headbody;
    private static Font keyfont;
    private static Font textfont;
    // 最大宽度
    private static int maxWidth = 360;

    // 静态代码块
    static {
        try {
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            //引入特殊字符的字体
            titlefont = new Font(bfChinese, 14, Font.BOLD);
            headfont = new Font(bfChinese, 12, Font.BOLD);
            headbody = new Font(bfChinese, 12, Font.NORMAL);
            keyfont = new Font(bfChinese, 10, Font.BOLD);
            textfont = new Font(bfChinese, 10, Font.NORMAL);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String createPDF(Map<String, Object> paramsMap) throws IOException {
        Document document = new Document(PageSize.A4);
        String fileName = UUID.randomUUID().toString();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_PATH + fileName + ".pdf"));
            document.addTitle("example of PDF");
            document.open();
            // 段落
            Paragraph paragraph = new Paragraph(ShiroUtil.getUserName() + "的个人简历", titlefont);
            paragraph.setAlignment(1); //设置文字居中 0靠左   1，居中     2，靠右
            paragraph.setIndentationLeft(12); //设置左缩进
            paragraph.setIndentationRight(12); //设置右缩进
            paragraph.setFirstLineIndent(24); //设置首行缩进
            //paragraph.setLeading(50f); //行间距
            paragraph.setSpacingBefore(5f); //设置段落上空白
            paragraph.setSpacingAfter(10f); //设置段落下空白
            document.add(paragraph);


            //增加块到文档
          /*  document.add(chunk1);
            document.add(chunk2);*/
            //短语
            Phrase phrase = new Phrase();
            Paragraph paragraph2 = new Paragraph(phrase);
            document.add(paragraph2);
            PdfPTable table = createTable(writer, paramsMap);
            //设置table的宽度并且锁定
            table.setTotalWidth(PageSize.A4.getWidth() - 100);
            table.setLockedWidth(true);
            document.add(table);

        } catch (FileNotFoundException | DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }

        return FILE_PATH + fileName + ".pdf";
    }

    //生成一个两列的表格
    public static PdfPTable createTable(PdfWriter writer, Map<String, Object> paramsMap) throws DocumentException, IOException {
        // 添加图片
        Image image = Image.getInstance("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fww3.sinaimg.cn%2Fmw690%2F0075YRjcly1h1xu7pwanhj30yi16mgtj.jpg&refer=http%3A%2F%2Fwww.sina.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1654409727&t=eb4356791d9e275fb056701f2c3aab9b");
        image.setAlignment(Image.ALIGN_CENTER);
        image.scalePercent(40); //依照比例缩放
        image.setAbsolutePosition(40, 60);

        PdfPTable table = new PdfPTable(9);

        table.addCell(createCell("个人信息", textfont, 1, 1, 3));

        table.addCell(createCell("个人姓名", textfont, 1, 1));
        table.addCell(createCell((String) paramsMap.get("name"), textfont, 1, 3, 1));

        table.addCell(createCell("照片", textfont, 1, 2, 3));
        table.addCell(createCell(image, textfont, 1, 2, 3));

        table.addCell(createCell("地址", textfont, 1, 1));
        table.addCell(createCell((String) paramsMap.get("address"), textfont, 1, 3, 1));

        table.addCell(createCell("电话", textfont, 1, 1));
        table.addCell(createCell((String) paramsMap.get("phone"), textfont, 1, 3, 1));


        table.addCell(createCell("求职意向", textfont, 1, 7, 1));
        table.addCell(createCell((String) paramsMap.get("attend"), textfont, 1, 2, 1));

        table.addCell(createCell("受教育程度", textfont, 1, 7, 1));
        table.addCell(createCell((String) paramsMap.get("education"), textfont, 1, 2, 1));

        table.addCell(createCell("职场身份", textfont, 1, 7, 1));
        table.addCell(createCell((String) paramsMap.get("identity"), textfont, 1, 2, 1));

        table.addCell(createCell("教育经历", textfont, 1, 7, 1));
        table.addCell(createCell((String) paramsMap.get("education_experience"), textfont, 1, 2, 1));

        table.addCell(createCell("获奖及荣誉", textfont, 1, 7, 1));
        table.addCell(createCell((String) paramsMap.get("honor"), textfont, 1, 2, 1));

        table.addCell(createCell("公司经历", textfont, 1, 7, 1));
        table.addCell(createCell((String) paramsMap.get("company"), textfont, 1, 2, 1));

        table.addCell(createCell("个人预期薪水", textfont, 1, 7, 1));
        table.addCell(createCell((String) paramsMap.get("salary"), textfont, 1, 2, 1));
        return table;
    }

    /**
     * ------------------------创建表格单元格的方法start----------------------------
     */
    public static PdfPCell createCell(Image image, Font font, int align, int colspan, int rowspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);
        cell.setFixedHeight(rowspan * 25f);
        cell.setImage(image);
        // cell.setPhrase(new Phrase(new Chunk(image, 0, 0,false)));
        return cell;
    }

    public static PdfPCell createCell(Image image, Font font, int align, int colspan, int rowspan, int borderWidthTop, int borderWidthRight, int borderWidthBottom, int borderWidthLeft) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);
        cell.setFixedHeight(rowspan * 25f);
        cell.setImage(image);
        //去除边框
        cell.setBorderWidthTop(borderWidthTop);
        cell.setBorderWidthRight(borderWidthRight);
        cell.setBorderWidthBottom(borderWidthBottom);
        cell.setBorderWidthLeft(borderWidthLeft);
        return cell;
    }

    /**
     * 创建单元格(指定字体)
     *
     * @param value
     * @param font
     * @return
     */
    public static PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, font));
        cell.setFixedHeight(25f);
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平..）
     *
     * @param value
     * @param font
     * @param align
     * @return
     */
    public static PdfPCell createCell(String value, Font font, int align) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        cell.setFixedHeight(25f);
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并）
     *
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @return
     */
    public static PdfPCell createCell(String value, Font font, int align, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    public static PdfPCell createCell(String value, Font font, int align, int colspan, int rowspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);
        cell.setFixedHeight(rowspan * 25f);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    public static PdfPCell createCell(String value, Font font, int align, int colspan, int rowspan, int borderWidthTop, int borderWidthRight, int borderWidthBottom, int borderWidthLeft) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);
        cell.setFixedHeight(rowspan * 25f);
        cell.setPhrase(new Phrase(value, font));
        //去除边框
        cell.setBorderWidthTop(borderWidthTop);
        cell.setBorderWidthRight(borderWidthRight);
        cell.setBorderWidthBottom(borderWidthBottom);
        cell.setBorderWidthLeft(borderWidthLeft);
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）
     *
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param boderFlag
     * @return
     */
    public static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setPadding(3.0f);
        if (!boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(15.0f);
            cell.setPaddingBottom(8.0f);
        } else if (boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(0.0f);
            cell.setPaddingBottom(15.0f);
        }
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平..、边框宽度：0表示无边框、内边距）
     *
     * @param value
     * @param font
     * @param align
     * @param borderWidth
     * @param paddingSize
     * @param flag
     * @return
     */
    public static PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setFixedHeight(25f);
        cell.setPhrase(new Phrase(value, font));
        cell.setBorderWidthLeft(borderWidth[0]);
        cell.setBorderWidthRight(borderWidth[1]);
        cell.setBorderWidthTop(borderWidth[2]);
        cell.setBorderWidthBottom(borderWidth[3]);
        cell.setPaddingTop(paddingSize[0]);
        cell.setPaddingBottom(paddingSize[1]);
        if (flag) {
            cell.setColspan(2);
        }
        return cell;
    }
/**------------------------创建表格单元格的方法end----------------------------*/


/**--------------------------创建表格的方法start------------------- ---------*/


    /**
     * 创建默认列宽，指定列数、水平(居中、右、左)的表格
     *
     * @param colNumber
     * @param align
     * @return
     */
    public static PdfPTable createTable(int colNumber, int align) {
        PdfPTable table = new PdfPTable(colNumber);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(align);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 创建指定列宽、列数的表格
     *
     * @param widths
     * @return
     */
    public static PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 创建空白的表格
     *
     * @return
     */
    public static PdfPTable createBlankTable() {
        PdfPTable table = new PdfPTable(1);
        table.getDefaultCell().setBorder(0);
        table.addCell(createCell("", keyfont));
        table.setSpacingAfter(20.0f);
        table.setSpacingBefore(20.0f);
        return table;
    }
/**--------------------------创建表格的方法end------------------- ---------*/
}